package day31.edu;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

// Created: 01/10/2025, ChangJiang Ru
public class FileStreamTest {
    @Test
    public void test1() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            // Create file object
            File srcFile = new File("boy.png");
            File destFile = new File("boy_copy.png");

            // Create Input Stream and Output Stream
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);

            // The data read input and write output
            byte[] byteBuffer = new byte[1024]; // 1kb
            int len;
            while ((len = fileInputStream.read(byteBuffer)) != -1) {
                fileOutputStream.write(byteBuffer, 0, len);
            }

            System.out.println("Copy successfully");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // Close the stream source
            try {
                if (fileInputStream != null)
                    fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (fileOutputStream != null)
                    fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
